package com.ease.framework.flex.controls {
   import flash.display.Graphics;
   import flash.geom.Point;

   import mx.core.UIComponent;

   /**
    * The dotted line.
    */
   public class DottedLine extends UIComponent {
      public var dash:int = 5;
      public var gap:int = 2;

      public var x1:int = 0;
      public var y1:int = 0;

      protected var commandStack:Array = [];

      public function DottedLine() {
         super();
      }

      public function set spoint(point:Point):void {
         x = point.x;
         y = point.y;
      }

      public function set epoint(point:Point):void {
         x1 = point.x;
         y1 = point.y;
      }

      public function draw(g:Graphics):void {
         var item:Object;
         for each (item in commandStack) {
            if (item.type == "m") {
               g.moveTo(item.x, item.y);
            }
            else {
               g.lineTo(item.x, item.y);
            }
         }
      }

      public function preDraw():void {
         commandStack = [];

         commandStack.push({type: "m", x: x, y: y});

         if (dash == 0 && gap == 0) {
         }
         else {
            var length:Number = Math.abs(Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2)));

            var count:int = 0;
            var currentLength:int = 0;
            var ratio:Number = 1;
            while (currentLength < length) {
               // dash
               if (count % 2 == 0) {
                  currentLength += dash;
                  ratio = Math.min(1, (currentLength / length));
                  //trace("currentLength: " + currentLength + " x: " + (x + ((x1-x)*ratio)));
                  commandStack.push({type: "l", x: x + ((x1 - x) * ratio), y: y + ((y1 - y) * ratio)});
               }
               else {
                  currentLength += gap;
                  ratio = Math.min(1, (currentLength / length));

                  commandStack.push({type: "m", x: x + ((x1 - x) * ratio), y: y + ((y1 - y) * ratio)});
               }
               count++;
            }
         }
      }
   }
}